import BigNumber from "bignumber.js"

export function formatNumber(value, options) {
    options = options || {
        FORMAT: {
          decimalSeparator: ".",
          groupSeparator: ",",
          groupSize: 3,
        },
      }
      BigNumber.config(options)
  
      const num = new BigNumber(value)
      return value? num.decimalPlaces(2, BigNumber.ROUND_DOWN).toFormat(2) : '0.00' // 保留2位小数并添加千分位
}

export function formatToYuan(value) {
  return value ? new BigNumber(value).dividedBy(100).toNumber() : "0.00"
}

// 防抖函数
/**
 * 
 * @param {*} func 
 * @param {*} wait 
 * @returns 
 */
export function debounce(func, wait) {
  let timeout
  return function () {
    const context = this
    const args = arguments
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.apply(context, args)
    }, wait)
  }
}